package com.ebelter.btcomlib.views.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.views.WheelView;

import java.util.List;

import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;
import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_CENTER;

/**
 * Created by pan on 2018/8/
 */

public class WheelViewDialog extends Dialog implements View.OnClickListener {


    private Context mContext;
    private int mGravity;

    private WheelView wheelView_dialog;
    private LinearLayout date_root_ll;
    private TextView tv_cancle;
    private TextView tv_select;

    private int offeset;
    private List<String> items;
    private WheelView.OnWheelViewListener onWheelViewListener;
    private int selectPosition;



    public WheelViewDialog(@NonNull Context context) {
        super(context,  R.style.dialog_style);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        this.mContext = context;
        mGravity=Gravity_CENTER;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final View view = View.inflate(mContext, R.layout.wheelview_dialog, null);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Window dialogWindow = getWindow();
                if (dialogWindow != null) {


                    if (mGravity==Gravity_BUTTOM){
                        dialogWindow.setGravity(Gravity.BOTTOM);
                    }else if (mGravity==Gravity_CENTER){
                        dialogWindow.setGravity(Gravity.CENTER);
                    }



                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    DisplayMetrics d = mContext.getResources().getDisplayMetrics();
                    // int maxOne = Math.max(d.widthPixels, d.heightPixels);
                    int minOne = Math.min(d.widthPixels, d.heightPixels);
                    lp.width = (int) (minOne);
//                    lp.height = (int) (minOne * 0.3);
                    // lp.width= DipUtil.dip2px(getContext(),288);
                    lp.height= LinearLayout.LayoutParams.WRAP_CONTENT;
                    dialogWindow.setAttributes(lp);
                }
            }
        });
        setContentView(view);
        FV();
        setListeners();
        initDatas();



    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public void setOffeset(int offeset) {
        this.offeset = offeset;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }

    public void setOnWheelViewListener(WheelView.OnWheelViewListener onWheelViewListener) {
        this.onWheelViewListener = onWheelViewListener;
    }

    public void setGravity(int gravity){
        this.mGravity=gravity;
    }

    private void initDatas() {

        wheelView_dialog.setOffset(offeset);
        wheelView_dialog.setItems(items);
        wheelView_dialog.setSeletion(this.selectPosition);
        wheelView_dialog.setOnWheelViewListener(onWheelViewListener);

    }

    private TextTipDialog.ICancelOkBtListener listener;

    public void setListener(TextTipDialog.ICancelOkBtListener listener) {
        this.listener = listener;
    }

    private void setListeners() {
        tv_cancle.setOnClickListener(this);
        tv_select.setOnClickListener(this);
    }

    private void FV() {
        date_root_ll =findViewById(R.id.date_root_ll);
        tv_cancle =findViewById(R.id.tv_cancle);
        tv_select =findViewById(R.id.tv_select);
        wheelView_dialog = findViewById(R.id.wheelView_dialog);
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancle) {
            if (listener != null) {
                listener.cancel(this);
            }
        } else if (v.getId() == R.id.tv_select) {
            if (listener != null) {
                listener.ok(this);
            }
        }

    }

    public void setBackgroundColor(int color){
        date_root_ll.setBackgroundColor(color);
    }



}
